package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.RepayRecord;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.utils.DateTimeUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.MyListView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by FHW
 * on 2017/11/13 11:32.
 */

public class RepaymentRecordAdapter extends CommonRecyclerAdapter<RepayRecord> {
    public static final int TYPE_NO_DATA = -1;
    public static final int TYPE_NORMAL = 0;

    public RepaymentRecordAdapter(Context context, List<RepayRecord> data) {
        super(context, data, R.layout.item_repayment_record);
    }

    @Override
    public void convert(CommonRecyclerHolder holder, final RepayRecord item) {
        holder.setTextViewText(R.id.tv_time, item.getTime());

        MyListView lv_record = holder.getView(R.id.lv_record);
        lv_record.setAdapter(new CommonAdapter<RepayRecord.RepayRecordsListsBean>(mContext, item.getRepayRecordsList(), R.layout.item_credit_repay_record) {
            @Override
            public void convert(ViewHolder holder, RepayRecord.RepayRecordsListsBean item, int position) {

                TextView tv_bank_name = holder.getView(R.id.tv_bank_name);
                TextView tv_time = holder.getView(R.id.tv_time);
                TextView tv_statue = holder.getView(R.id.tv_statue);
                TextView tv_money = holder.getView(R.id.tv_money);

                ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + item.getPicture(), (ImageView) holder.getView(R.id.iv_pic));
                String cardId = item.getCredit_card_no();
                tv_bank_name.setText(item.getCard_type_name() + "(尾号" + cardId.substring(cardId.length() - 4, cardId.length()) + ")");
                if (!item.getRepayTime().equals("")) {
                    String dayStr = DateTimeUtil.getDateDayTimeNew(DateTimeUtil.getDate(item.getRepayTime()));
                    tv_time.setText(dayStr);
                }
                if (!item.getRepayment_amount().equals("")) {
                    DecimalFormat df = new DecimalFormat("#0.00");
                    tv_money.setText(df.format(Double.parseDouble(item.getRepayment_amount())));
                }
                tv_money.setTextColor(mContext.getResources().getColor(R.color.color_222222));
                tv_statue.setTextColor(mContext.getResources().getColor(R.color.color_222222));
                switch (item.getState()) {
                    case 1:
                        tv_statue.setText("还款成功");
                        break;
                    case 2:
                        tv_statue.setText("还款失败");
                        break;
                    case 3:
                        tv_statue.setText("还款中");
                        tv_statue.setTextColor(mContext.getResources().getColor(R.color.color_ff7373));
                        break;
                    case 4:
                        tv_statue.setText("退款成功(2-7个工作日到账)");
                        break;
                    case 5:
                        tv_statue.setText("退款失败");
                        tv_money.setTextColor(mContext.getResources().getColor(R.color.color_999999));
                        tv_statue.setTextColor(mContext.getResources().getColor(R.color.color_AFAFAF));
                        break;
                    case 6:
                        tv_statue.setText("退款中(2-7个工作日到账)");
                        break;
                    default:
                        break;
                }
            }
        });

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_NO_DATA) {
            View root = inflater.inflate(R.layout.item_no_data, parent, false);
            root.setVisibility(View.VISIBLE);
            ((ImageView) root.findViewById(R.id.iv_no_data_pic)).
                    setImageResource(R.mipmap.img_no_record);
            ((TextView) root.findViewById(R.id.tv_no_data_des)).
                    setText("无收款记录");
            return new RecyclerView.ViewHolder(root) {
            };
        }
        if (viewType == TYPE_NORMAL) {
            View root = inflater.inflate(R.layout.item_repayment_record, parent, false);
            return new CommonRecyclerHolder(root);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return (mData == null || mData.size() == 0) ? 1 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return (mData == null || mData.size() == 0) ? TYPE_NO_DATA : TYPE_NORMAL;
    }

}
